/**
 * https://leetcode.cn/problems/max-area-of-island/submissions/576384318/
 * 695. 岛屿的最大面积
 * medium,陈贝纯 2024.10.28
 * 并查集
 */

class Solution {
public:
    int sum=0;
    int n;
    int m;
    void dfs(vector<vector<int>>& grid,int i,int j){
        grid[i][j]=0;
        if(i+1<n&&grid[i+1][j]==1){
            sum++;
            dfs(grid,i+1,j);
        }
        if(i-1>=0&&grid[i-1][j]==1){
            sum++;
            dfs(grid,i-1,j);
        }
        if(j+1<m&&grid[i][j+1]==1){
            sum++;
            dfs(grid,i,j+1);
        }
        if(j-1>=0&&grid[i][j-1]==1){
            sum++;
            dfs(grid,i,j-1);
        }
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        n=grid.size();
        m=grid[0].size();
        int num=0;
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(grid[i][j]==1){
                    sum=1;
                    dfs(grid,i,j);
                    num=max(sum,num);
                }
            }
        }
        return num;
    }
};